package com.open.capacity.study.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysTeaching;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.study.service.TeachingSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.open.capacity.log.annotation.LogAnnotation;

import java.util.Map;


@RestController
@RequestMapping("/teachingSys")
@Api(tags = "TEACHING API")
public class TeachingSystemController {

    @Autowired
    private TeachingSystemService teachingSystemService;

    /**
     * 增加教学体系
     * @param sysTeaching
     * @return
     * @throws ControllerException
     */
    @PostMapping("/insert")
    //权限注解（暂无）
    //@PreAuthorize("hasAnyAuthority('')")
    @ApiOperation(value = "添加体系可用状态")
    @LogAnnotation(module = "study-center", recordRequestParam = false)
    public Result insert(@RequestBody SysTeaching sysTeaching) throws ControllerException {
        try {
            return teachingSystemService.insert(sysTeaching);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

    /**
     * 修改可用状态
     * @param params
     * @return
     * @throws ControllerException
     */
        @PostMapping("/delete")
        @ApiOperation(value = "修改体系可用状态")
        //权限注解（暂无）
        //@PreAuthorize("hasAnyAuthority('')")
        @LogAnnotation(module = "study-center", recordRequestParam = false)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "tcstid", value = "体系id", required = true, dataType = "Integer"),
                @ApiImplicitParam(name = "status", value = "是否启用", required = true, dataType = "int")
        })
        public Result updateStatus(@RequestParam Map<String,Object> params) throws ControllerException{
            try {
                return teachingSystemService.updateStatus(params);
            }catch (ServiceException e){
                throw new ControllerException(e);
            }
        }

    /**
     * 修改教学体系信息
     * @param sysTeaching
     * @return
     * @throws ControllerException
     */
    @PostMapping("/update")
    //权限注解（暂无）
    //@PreAuthorize("hasAnyAuthority('')")
    @LogAnnotation(module = "study-center", recordRequestParam = false)
    public Result update(@RequestBody SysTeaching sysTeaching) throws ControllerException{
        try {
            return teachingSystemService.update(sysTeaching);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

    /**
     * 教学体系列表查询
     * @param params
     * @return
     * @throws ControllerException
     */
    @GetMapping("/getList")
    @ApiOperation(value = "体系查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    //权限注解（暂无）
    //@PreAuthorize("hasAnyAuthority('')")
    @LogAnnotation(module = "study-center", recordRequestParam = false)
    public PageResult<SysTeaching> findTeaching(@RequestParam Map<String, Object> params) throws ControllerException{
        try {
            return teachingSystemService.findTeaching(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @GetMapping("/getList/{id}")
    @LogAnnotation(module = "study-center", recordRequestParam = false)
    public SysTeaching findById(@PathVariable Integer id) throws ControllerException{
        try {
            return teachingSystemService.findById(id);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }
}
